﻿Public Class MainForm
    Public defaultHome As UserControl
    Public history As New ArrayList
    Dim currentView As baseForm = Nothing
    Dim iCurrentView As Integer = 0
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Session.dbname = My.Settings.dbname
        Session.dbhost = My.Settings.dbhost
        Session.dbport = My.Settings.dbport
        Session.dbuser = My.Settings.dbuser
        Session.dbpassword = My.Settings.dbpassword

        lblTitle.Text = My.Application.Info.ProductName
        'Dim WelcomeForm As New WelcomeForm
        enterView("WelcomeForm")
        checkDatabaseStatus()
        setLoggedIn(False)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WindowState = FormWindowState.Minimized


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()

    End Sub

    Public Sub enterView(ByVal fname As String)
        Dim bf As baseForm = DynamicallyLoadedObject(fname)
        enterView(bf, False)
    End Sub
    Public Sub enterView(ByVal view As baseForm, Optional ByVal skipHistory As Boolean = False)
        'save current view to stack, so that we can navigate back to this view
        If Not currentView Is Nothing Then
            history.Add(currentView)
        End If

        If Not view Is Nothing Then
            pnlUtama.Controls.Clear()
            pnlUtama.Controls.Add(view)
            view.AutoScroll = True
            view.Dock = DockStyle.Fill
            view.BorderStyle = BorderStyle.None
            'AddHandler view.MouseDown, AddressOf Form1_MouseDown
            currentView = view

            If Not skipHistory Then 'skip if we come to this view using navigational button
                history.Add(view) 'this way, currentView is added to list
                iCurrentView = history.Count - 1
                breadCumb.add(view)
            End If
        End If
    End Sub
    Private Sub breadCumb_EnterPage(ByVal sender As Object, ByVal e As System.EventArgs) Handles breadCumb.EnterPage
        Dim page As baseForm = sender
        enterView(page, True)
    End Sub
    Public Sub setFirstNavigation(ByVal fname As String)
        Dim page As baseForm = DynamicallyLoadedObject(fname)
        history.Clear()
        breadCumb.clearNode()
        createActivities(page)
        enterView(page)
        defaultHome = page
    End Sub
    
    Private Sub createActivities(ByRef page As baseForm)
        Dim dtmenu As New DataTable
        Dim groups As Hashtable = New Hashtable
        If Utils.executeSP("proc_z_getrolemenu", New Object() {"role", Session.kodeRole}, dtmenu) Then
            If dtmenu.Rows.Count > 0 Then
                If page.lvPageActivities IsNot Nothing Then
                    page.lvPageActivities.View = View.SmallIcon 'defaulted, change to user saved
                    page.lvPageActivities.HoverSelection = True
                    page.lvPageActivities.HotTracking = True
                    page.lvPageActivities.Activation = ItemActivation.OneClick
                    page.lvPageActivities.Groups.Clear()
                    AddHandler page.lvPageActivities.ItemActivate, AddressOf activateActivity

                End If
                For Each row As DataRow In dtmenu.Rows
                    If row("header") = 0 Then
                        If page.lvPageActivities IsNot Nothing Then
                            Dim group As ListViewGroup = New ListViewGroup(row("menuname"))
                            page.lvPageActivities.Groups.Add(group)
                            groups.Add("" & row("menuid"), group)
                        End If
                    Else
                        If page.lvPageActivities IsNot Nothing Then
                            Dim item As ListViewItem = page.lvPageActivities.Items.Add(row("menuname"), CInt(row("imageid")))
                            Dim groupId As String = row("header")
                            groupId = groupId.Substring(0, 1)
                            item.Tag = row("formname")
                            item.Group = groups(groupId)
                            'item.SubItems.Add(row("description"))


                            'End If
                        End If
                    End If
                Next
                For Each g As ListViewGroup In page.lvPageActivities.Groups
                    If g.Header.ToLower = "default" Then
                        MsgBox(g.Header)
                        Exit For
                    End If

                Next
            End If
        End If
    End Sub

    Private Sub activateActivity(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim item As ListViewItem
        Try
            item = currentView.lvPageActivities.SelectedItems(0)
            enterView(item.Tag)
        Catch ex As Exception
            MsgBox("Application Error : no page defined for action : " & item.Tag)
        End Try
    End Sub

    Public Sub logout()
        If MessageBox.Show("Anda yakin akan logout?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Session.isLoggedIn = False
            'Main.defaultHome = Pages.home
            'TODO : clear additional items here
            history.Clear()
            breadCumb.clearNode()
            enterView("WelcomeForm")
        End If
    End Sub
    Public Sub checkDatabaseStatus()
        My.Settings.dbname = Session.dbname
        My.Settings.dbhost = Session.dbhost
        My.Settings.dbport = Session.dbport
        My.Settings.dbuser = Session.dbuser
        My.Settings.dbpassword = Session.dbpassword
        My.Settings.Save()

        If Utils.isConnected() Then
            picDatabaseStatus.Image = My.Resources.database_up
            lblDatabaseStatus.Text = My.Settings.dbname & "@" & My.Settings.dbhost
        Else
            If My.Settings.dbname = "" Then
                lblDatabaseStatus.Text = "<NOT DEFINED YET>@" & My.Settings.dbhost
            Else
                lblDatabaseStatus.Text = My.Settings.dbname & "@" & My.Settings.dbhost
            End If
            picDatabaseStatus.Image = My.Resources.database_down
        End If
    End Sub

    Public Sub setLoggedIn(ByVal value As Boolean)
        Session.isLoggedIn = value
        If Session.isLoggedIn Then
            link_logout.Visible = True
        Else
            link_logout.Visible = False
        End If
    End Sub

    Public Function DynamicallyLoadedObject(ByVal objectName As String, Optional ByVal args() As Object = Nothing) As Control
        Dim returnObj As Object = Nothing
        Dim ProjectName As String = Reflection.Assembly.GetExecutingAssembly.GetName.Name
        Dim Type As Type = Reflection.Assembly.GetExecutingAssembly().GetType(ProjectName & "." & objectName)
        If Type IsNot Nothing Then
            returnObj = Activator.CreateInstance(Type, args)
        End If
        Return returnObj
    End Function

    Private Sub link_logout_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles link_logout.LinkClicked
        logout()
    End Sub
End Class
